package com.marvels.financial.loan.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.LoanAuditConditionDto;
import com.marvels.financial.loan.service.LoanAuditApi;

/**
 * 放款审核
 * @author tangpw
 *
 */
@RestController
@RequestMapping("financial/loan/loanAudit/service")
public class LoanAuditController {

	@Autowired
	private LoanAuditApi loanAuditApi;



	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}


	/**
	 * 查询需要审核的放款列表(处于放款申请状态)
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("auditList")
	@ResponseBody
	public MarvelsResultDTO auditList(@RequestBody LoanAuditConditionDto loanAuditConditionDto) {
		try {
			return loanAuditApi.applyingList(loanAuditConditionDto);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询需要审核的放款列表(处于放款申请状态)FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询需要审核的放款列表(处于放款申请状态)异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询需要审核的放款列表(处于放款申请状态)异常", null);
		}
	}


	/**
	 * 放款金额调整
	 * @param loanAuditConditionDto
	 * @return
	 */
	@RequestMapping("loanAmountAdjust")
	@ResponseBody
	public MarvelsResultDTO loanAmountAdjust(@RequestBody LoanAuditConditionDto loanAuditConditionDto) {
		try {
			loanAuditApi.loanAmountAdjust(loanAuditConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放款金额调整处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放款金额调整异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "放款金额调整异常", null);
		}
	}


	/**
	 * 审核通过
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("loanAuditPass")
	@ResponseBody
	public MarvelsResultDTO loanAuditPass(@RequestBody LoanAuditConditionDto loanAuditConditionDto) {
		try {
			loanAuditConditionDto.setOperationStatus("1");
			loanAuditConditionDto.setLendType("2");//手工放款
			return loanAuditApi.executeApplyAudist(loanAuditConditionDto);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>审核通过处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>审核通过处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "审核通过处理异常", null);
		}
	}






	/**
	 * 审核拒绝
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("loanAuditRefuse")
	@ResponseBody
	public MarvelsResultDTO loanAuditRefuse(@RequestBody LoanAuditConditionDto loanAuditConditionDto) {

		if (StringUtils.isEmpty(loanAuditConditionDto.getLoanIds())) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), "请至少选择一项进行审核", null);
		}
		loanAuditConditionDto.setOperationStatus("2");
		try {
			return loanAuditApi.executeApplyAudist(loanAuditConditionDto);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>审核拒绝处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>审核拒绝处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "审核拒绝处理异常", null);
		}
	}



	/**
	 * 审核取消
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("loanAuditCancle")
	@ResponseBody
	public MarvelsResultDTO loanAuditCancle(@RequestBody LoanAuditConditionDto loanAuditConditionDto) {

		if (StringUtils.isEmpty(loanAuditConditionDto.getLoanIds())) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), "请至少选择一项进行审核", null);
		}
		loanAuditConditionDto.setOperationStatus("3");
		try {
			return loanAuditApi.executeApplyAudist(loanAuditConditionDto);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>审核取消处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>审核取消处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "审核取消处理异常", null);
		}
	}


	/**
	 * 手动代付
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("handlePay")
	@ResponseBody
	public MarvelsResultDTO handlePay(@RequestBody LoanAuditConditionDto loanAuditConditionDto) {
		loanAuditConditionDto.setOperationStatus("4");
		try {
			return loanAuditApi.executeApplyAudist(loanAuditConditionDto);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>手动代付处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>手动代付处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "审核通过处理异常", null);
		}
	}







	/**
	 * 下载放款申请数据
	 * @param loanAuditConditionDto
	 */
	@RequestMapping("downLoad")
	public void downLoad(LoanAuditConditionDto loanAuditConditionDto,HttpServletRequest request,HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		OutputStream outs = null;
		try {
			response.setHeader("Content-disposition", "attachment; filename="+ new String("放款审核数据一览表.xls".getBytes("UTF-8"),"ISO8859-1"));
			HSSFWorkbook downLoadDeal2 = loanAuditApi.downLoadXsl(loanAuditConditionDto);
			outs = response.getOutputStream();// 获取文件输出IO流
			downLoadDeal2.write(outs);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载放款申请数据", e);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载放款申请数据", e);
		}finally{
			try {
				outs.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
